var ratPack = $.sammy(function() {

	this.element_selector = '#FormController';

	this.get('#/login', function(context) {
		
		user.teamplate.GetHtmlLogin(context);
		
		
	});
	this.get('#/register', function(context) {
		user.teamplate.GetHtmlRegister(context);
		
	});
	this.get('#/forgot', function(context) {
		user.teamplate.GetHtmlForgot(context);
		
	});	
	
	


	
});

function loadLadda(){
	try{
		user.ladda = Ladda.bind( 'button' );
	}catch(e){
		console.log(e.message);
	}
}
$(function() {

	
	ratPack.run('#/login');
	
	
	$('.register').live('click',function(e){
		var validate = user.actions.register.validate($("#register"));
		if(validate){
			user.actions.register.submit($("#register"),this);
		}
	});	
	$('.forgot').live('click',function(e){
		var validate = user.actions.forgot.validate($("#forgot"));
		if(validate){
			user.actions.forgot.submit($("#forgot"),this);
		}
	});
	$('.login').live('click',function(e){
		var validate = user.actions.login.validate($("#login"));
		if(validate){
			user.actions.login.submit($("#login"),this);
		}
		
	});
	
});


var user = {
	ladda:null,
	actions:{
	
		login:{
			validate:function(obj){
				
				return true;
			},
			submit:function(obj,_this){
				var error_login = $(".error-login");
				var data = obj.serialize();
				var loading = Ladda.create(_this);
				loading.start();
				$.post("/users/login",data,function(res){
					
					if(res.length == 0){
					
					}else{
						var i = 0;
						if(typeof res.code != "undefined"){
							if(res.code==400){
								error_login.html(res.message);
								
							}else{
								
								try{
									obj[0].reset();
									error_login.html('');
								}catch(e){
									console.log(e.message);
								}
								window.location.href = "/dashboard";
							}
						}else{
							$.each(res,function(x,y){
								if(i == 0){
									error_login.html(y[0]);
								}
								i++;
							});
						}
						
						
					}
					loading.stop();
					
				}).fail(function(xhr) {
					console.log(xhr);
					loading.stop();
				});
				return false;
			}
		},
		register:{
			validate:function(obj){
				
				return true;
			},
			submit:function(obj,_this){
				var error_register = $(".error-register");
				var data = obj.serialize();
				
				var loading = Ladda.create(_this);
				loading.start();
				$.post("/users/register",data,function(res){
					
					if(res.length == 0){
					
					}else{
						
						var i = 0;
						var httpCode = 0;
						if(typeof res.code != "undefined"){
							if(res.code==400){
								error_register.html(res.message);
								
							}else{
								
								try{
									obj[0].reset();
									error_register.html('');
								}catch(e){
									console.log(e.message);
								}
							}
						}else{
							$.each(res,function(x,y){
								if(i == 0){
									error_register.html(y[0]);
								}
								i++;
							});
						}
					}
					loading.stop();
					
				}).fail(function(xhr) {
					console.log(xhr);
					loading.stop();
				});
				return false;
			}
		},
		forgot:{
			validate:function(obj){
				
				return true;
			},
			submit:function(obj,_this){
				var error_forgot = $(".error-forgot");
				var data = obj.serialize();
				
				var loading = Ladda.create(_this);
				loading.start();
				$.post("/users/forgot",data,function(res){
					if(res.length == 0){
					
					}else{
						
						var i = 0;
						var httpCode = 0;
						if(typeof res.code != "undefined"){
							if(res.code==400){
								error_forgot.html(res.message);
								
							}else{
								
								try{
									obj[0].reset();
									error_forgot.html('');
								}catch(e){
									console.log(e.message);
								}
							}
						}else{
							$.each(res,function(x,y){
								if(i == 0){
									error_forgot.html(y[0]);
								}
								i++;
							});
						}
					}
					loading.stop();
				
				}).fail(function(xhr) {
					console.log(xhr);
					loading.stop();
				});
				return false;
			}
		}
		
	
	},
	teamplate:{
		GetHtmlLogin: function(context){
		
			$.get(homeURL+"/users/login",function(html){
				context.app.swap('');
				context.$element().html(html);
				try{
					user.animation();
				}catch(e){
				
				}
			});
		
		},
		GetHtmlRegister: function(context){
		
			$.get(homeURL+"/users/register",function(html){
				context.app.swap('');
				context.$element().html(html);
				try{
					user.animation();
				}catch(e){
				
				}
			});
		
		},
		GetHtmlForgot: function(context){
		
			$.get(homeURL+"/users/forgot",function(html){
				context.app.swap('');
				context.$element().html(html);
				try{
					user.animation();
				}catch(e){
				
				}
			});
		
		}
	},
	animation:function(){
		$("input").iCheck({
			checkboxClass: 'icheckbox_square-blue',
			increaseArea: '20%' // optional
		});
		$(".dark input").iCheck({
			checkboxClass: 'icheckbox_polaris',
			increaseArea: '20%' // optional
		});
		$(".form-control").focus(function () {
			$(this).closest(".textbox-wrap").addClass("focused");
		}).blur(function () {
			$(this).closest(".textbox-wrap").removeClass("focused");
		});

		//On Scroll Animations


		if ($(window).width() >= 968 && !Modernizr.touch && Modernizr.cssanimations) {

			$("body").addClass("scroll-animations-activated");
			$('[data-animation-delay]').each(function () {
				var animationDelay = $(this).data("animation-delay");
				$(this).css({
					"-webkit-animation-delay": animationDelay,
					"-moz-animation-delay": animationDelay,
					"-o-animation-delay": animationDelay,
					"-ms-animation-delay": animationDelay,
					"animation-delay": animationDelay
				});

			});
			$('[data-animation]').waypoint(function (direction) {
				if (direction == "down") {
					$(this).addClass("animated " + $(this).data("animation"));

				}
			}, {
				offset: '90%'
			}).waypoint(function (direction) {
				if (direction == "up") {
					$(this).removeClass("animated " + $(this).data("animation"));

				}
			}, {
				offset: $(window).height() + 1
			});
		}




		$(".colorBg a[href]").click(function () {
			var scrollElm = $(this).attr("href");
			try{
				$("html,body").animate({ scrollTop: $(scrollElm).offset().top }, 500);
			}catch(e){
			
			}
			return false;
		});
		
		$("form").submit(function (e) {
			e.preventDefault();
			return false;
		});
		loadLadda();
	}
};